/**
 * Created by gongxun on 17/1/13.
 */

var NnTableLayer = GameCCSBase.extend({
    mNnTableCardLayer : null,
    mNnTableUILayer : null,
    mKwxApplyTableLayer : null,
    mNnTableRoomEndLayer : null,
    mNnTableReplayLayer : null,

    //牌桌状态 0录像 1正常打牌
    mPlayerType : null,

    //统计打了多少局
    mPlayGameCount : null,

    //游戏规则
    mGameConfig : null,

    //房间号
    mGroupId : null,

    //群号
    mGroupName : null,

    //录像号
    mReplayId : null,

    //密码
    mTableEnterPwd : null,

    //是否是创建者
    mIsRoomCreator : null,
    ctor : function(){
        this._super();
        this.loadCocostudio("res/ccs/NnTable.json");
        mo.nnTable = this;
        this.mIsRoomCreator = false;

        this.loadUI();
    },

    onEnter : function(){
        this._super();
        this.runAction(cc.sequence(cc.delayTime(1), cc.callFunc(function(){
            mo.soundHelper.playMusicGameNn();
        })));

        mo.eventHelper.add("NnForceExitTableResponsennOK", this, function () {
            if (mo.nnTable.mGameConfig.maxpan == mo.nnTableStatus.mTableStatus.LeftPlayTimes){
                mo.showMsgDialogOk("退出牌局成功", function(){
                    this.quitNnGame();
                }, this);
            }
        });

        mo.eventHelper.add("CancleJuRequestnn", this, function(cancleJu){
            this.mKwxApplyTableLayer.updateCancelJuInfo(cancleJu);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    //正常打牌
    loadPlayCard : function(){
        this.mPlayGameCount = 0;
        this.mPlayerType = 1;
        this.mNnTableUILayer.intoShowIndex(10);

        //隐藏录像界面
        this.mNnTableReplayLayer.setVisible(false);

        //隐藏录像时候的ui
        this.mNnTableUILayer.setIsReplay(false);
    },

    //播放录像
    loadReplay : function(rePlaynn, replayId){
        this.mPlayGameCount = 0;
        this.mPlayerType = 0;
        this.mNnTableUILayer.intoShowIndex(10);

        //录像规则
        mo.nnTable.setNnConfig(rePlaynn.Result.CfgJson, "", "");

        //打开录像界面 并且播放
        this.mNnTableReplayLayer.setVisible(true);
        this.mNnTableReplayLayer.setReplayId(replayId);
        this.mNnTableReplayLayer.onStart();

        //隐藏录像时候的ui
        this.mNnTableUILayer.setIsReplay(true);

        this.mReplayId = replayId;
        mo.nnTableStatus.setReplayMsg(rePlaynn);
        mo.nnTableStatus.playReplay();
    },

    //显示代开房人
    setShowRoomCreate : function(user){
        if (user != null){
            var kwxDaikaifangWidget = new KwxDaikaifangWidget();
            kwxDaikaifangWidget.setUserInfo(user);
            this.addChild(kwxDaikaifangWidget);
            kwxDaikaifangWidget.setPosition(cc.p(mo.gameSize.x - 250, mo.gameSize.y - 10));
        }
    },

    loadUI : function(){
        //sprite
        if (mo.gameHelper.developerVersion == "3.0"){
            cc.spriteFrameCache.addSpriteFrames("res/glowCards.plist", "res/glowCards.png");
        }else if (mo.gameHelper.developerVersion == "2.0"){
            cc.spriteFrameCache.addSpriteFrames("res/pokerCards.plist", "res/pokerCards.png");
        }
        cc.spriteFrameCache.addSpriteFrames("res/emojis.plist", "res/emojis.png");
        cc.spriteFrameCache.addSpriteFrames("res/emojis_index.plist", "res/emojis_index.png");

        //牌层
        this.mNnTableCardLayer = new NnTableCardLayer();
        this.addChild(this.mNnTableCardLayer);

        //ui层
        this.mNnTableUILayer = new NnTableUILayer();
        this.addChild(this.mNnTableUILayer);

        //取消
        this.mKwxApplyTableLayer = new KwxApplyTableLayer("nn");
        this.addChild(this.mKwxApplyTableLayer);

        //录像层
        this.mNnTableReplayLayer = new NnTableReplayLayer();
        this.addChild(this.mNnTableReplayLayer);
    },

    setNnConfig : function(Cfgjson, tablePassWord, GroupName){
        this.mGroupName = GroupName;
        this.mTableEnterPwd = tablePassWord;
        this.mGameConfig = JSON.parse(Cfgjson);

        //info
        var infoStr = "";
        var json = this.mGameConfig;
        infoStr += (json.ruleid == 1 ? "大众玩法\n" : "随州玩法\n");
        infoStr += (json.maxpan + "局" + "\n");
        var hosttypeStr = ["轮庄", "连庄", "霸王庄"];
        infoStr += (hosttypeStr[json.hosttype] + "\n");
        if (json.addratetime == 0){
            infoStr += "每次选分";
        }else{
            infoStr += "固定" + json.basepoint + "分";
        }
        if (json.lod != 0){
            infoStr += "\n生死战:" + json.lod;
        }
        mo.gameHelper.setTextString(mo.seekWidgetByName(this.ccsNode, "Text_tableRule"), "" + infoStr);
    },

    quitNnGame : function(){
        if (mo.gameHelper.developerVersion == "2.0"){
            mo.sceneHelper.runSceneHall();
        }else if (mo.gameHelper.developerVersion == "3.0"){
            mo.sceneHelper.exitGameLayer();
            if (this.mGroupId != null){
                mo.hallLayer.intoGroup(this.mGroupId);
            }
        }
    },

    //发牌
    setFapaiAnim : function(cardNumss){
        this.mNnTableCardLayer.setFapaiAnim(cardNumss);
        this.mNnTableUILayer.setFapaiAnim(cardNumss);
    },

    //开牌 -1不显示
    setkaiPaiAnim : function(cardTypes, cardNumss){
        this.mNnTableCardLayer.setkaiPaiAnim(cardTypes, cardNumss);
        this.mNnTableUILayer.setkaiPaiAnim(cardTypes);
    },

    //开牌 开一个玩家的
    setKaipaiOnePlayer : function(cardType, cardNums, seat, isAnim){
        this.mNnTableCardLayer.setKaipaiOnePlayer(cardType, cardNums, seat, isAnim);
        this.mNnTableUILayer.setKaipaiOnePlayer(cardType, cardNums, seat);
    },

    //大结算
    onShowJuEndedLayer : function(juEndedEventnn){
        //判断是否需要直接退出 如果剩余局数和设置里面的局数一致 就直接退出
        var ruleJson = JSON.parse(juEndedEventnn.CfgJson);

        if (mo.nnTableStatus.mTableStatus.LeftPlayTimes == ruleJson.maxpan){
            mo.showMsgDialogOk("牌局已经解散", function(){
                this.quitNnGame();
            }, this);
        }else{
            this.mNnTableRoomEndLayer = new NnTableRoomEndLayer();
            mo.sceneHelper.showEndInfoLayer(this.mNnTableRoomEndLayer);
            // this.addChild(this.mNnTableRoomEndLayer);
            this.mNnTableRoomEndLayer.setRoomEndInfo(juEndedEventnn);

            if (juEndedEventnn.EndFlag != 1){
                //自然结束 通过当局结算打开
                this.mNnTableRoomEndLayer.setVisible(false);
            }else{
                this.mNnTableRoomEndLayer.setVisible(true);
            }
        }
    },

    //更新剩余牌局
    updateLastPlayTimes : function(){
        var time = mo.nnTableStatus.mTableStatus.LeftPlayTimes;
        if (time != null){
            if (time < 0){
                time = 0;
            }
            mo.seekWidgetByName(this.ccsNode, "AtlasLabel_leftJu").setString("" + time);
            if (mo.gameHelper.developerVersion == "2.0"){
                mo.seekWidgetByName(this.ccsNode, "AtlasLabel_roomId").setString("" + mo.nnTableStatus.mTableStatus.TableID);
            }
        }
    },

    //邀请玩家
    onInvite : function(){
        var gameName = "nn";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，就等你来了(点击加入)";
        var tableId = mo.nnTableStatus.mTableStatus.TableID;

        var title = "桌号:[" + tableId + "]" + "-" + mo.gameHelper.projectName +"-牛牛";
        var description = configSummary;
        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(1, tableId));
    },

    //分享录像
    onShareReplay : function(){
        var gameName = "nn";
        var configSummary = mo.gameHelper.getRuleConfigSummary(this.mGameConfig, gameName);
        configSummary += "，在游戏中输入录像id就能看到我的精彩回放了";

        var title = "录像id:" + this.mReplayId.substr(7) + "-" + mo.gameHelper.projectName + "-牛牛";
        var description = configSummary;

        mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, this.mReplayId.substr(7)));
    }
});